package com.latefly.skewwork.activity// 引用所需的包
import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.bluetooth.le.BluetoothLeScanner
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanFilter
import android.bluetooth.le.ScanResult
import android.bluetooth.le.ScanSettings
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import com.latefly.skewwork.utils.PermissionUtils
import com.latefly.skewwork.ui.theme.SkewWorkTheme

//蓝牙扫描
//参考：https://blog.51cto.com/u_16213406/7169778


class BleActivity : ComponentActivity() {

    private lateinit var bluetoothAdapter: BluetoothAdapter
    private lateinit var bluetoothLeScanner: BluetoothLeScanner


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        PermissionUtils.checkPermissionWithRequire(this)

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

        // 检查蓝牙权限
        if (!PermissionUtils.checkBlePermission(this, Manifest.permission.BLUETOOTH)) {
            PermissionUtils.checkPermissionWithRequire(this, Manifest.permission.BLUETOOTH)
            startBleScan()
        }

        setContent {
            SkewWorkTheme {
                Surface(
                    modifier = Modifier
                        .wrapContentHeight()
                        .wrapContentWidth(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Text("Ble test!")
                }
            }
        }


    }

    private fun checkBtConPermission(permission: String): Boolean {
        return PermissionUtils.checkPermissionWithRequire(this, permission)
    }

    private val scanCallback = object : ScanCallback() {
        override fun onScanResult(callbackType: Int, result: ScanResult?) {
            super.onScanResult(callbackType, result)
            // 处理扫描结果
            result?.device?.let {

                if (checkBtConPermission(Manifest.permission.BLUETOOTH_CONNECT)) {
                    Log.d("BLE", "Device： ${it.address} Data:${it.address}")
                }


            }
        }
    }

    private fun startBleScan() {
        // 检查定位权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
            PermissionUtils.checkBlePermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        ) {
            PermissionUtils.requirePermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        } else {
            bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
            val scanSettings = ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .build()
            val scanFilters = mutableListOf<ScanFilter>()
            bluetoothLeScanner.startScan(scanFilters, scanSettings, scanCallback)
        }
    }

    override fun onStop() {
        super.onStop()
        if (checkBtConPermission(Manifest.permission.BLUETOOTH_SCAN)) {
            return
        }
        bluetoothLeScanner.stopScan(scanCallback)
    }
}